/*
 * System.c
 *
 *  Created on: 30.11.2011
 *      Author: Christian St�veken christian.Stoeveken@googlemail.com
 *      Author: Jonas Wilhelm wilheljo@googlemail.com
 *      Author: Jannik Müller jannik-mpunkt@web.de
 */

#include "System.h"
#include "Sensors.h"

//storage dummy: simulates the "real" storage to load from / save to
struct slotValuesInStorage storageDummy[7];

//feed the storage dummy some values
void bootstrap(){
    int i;
    for(i=0;i<7;i++){
        storageDummy[i].interval = 30;
        storageDummy[i].dose = i*3;
        storageDummy[i].slotActive = i%2;
    }
    printf("\nActivated metering for all slots\n");
}

//Store the value contained in "iValue" as the "valueType" of slot "slotNumber"
void storeValue(int slotNumber, enum PARAM valueType, int iValue) {
    
    switch(valueType){
        case TIME_INTERVAL:
            storageDummy[slotNumber].interval = iValue;
            break;
        case DOSE:
            storageDummy[slotNumber].dose = iValue;
            break;
        case SLOT_ACTIVE:
            storageDummy[slotNumber].slotActive = iValue;
            break;
        default:
            printf("storeValue() is called with an unsupported valueType\n");
            exit(-1);
    }

	printf("Saved value %i for slot %d\n", iValue, slotNumber);
        
};


//Load the "valueType"-value of slot "slotNumber" from storage
//returns the result as an int
int loadValue (int slotNumber, enum PARAM valueType) {
	int iValue = 3;
    
     switch(valueType){
        case TIME_INTERVAL:
            iValue = storageDummy[slotNumber].interval;
            printf("Loaded value %i as interval for slot %i from (fake) storage\n", iValue, slotNumber+1);
            break;
        case DOSE:
            iValue = storageDummy[slotNumber].dose;
            printf("Loaded value %i as dose (ml) for slot %i from (fake) storage\n", iValue, slotNumber+1);
            break;
        case SLOT_ACTIVE:
            iValue = storageDummy[slotNumber].slotActive;
            if(iValue == 0)     printf("Slot %i is deactivated\n", slotNumber);
            else                printf("Slot %i is activated\n", slotNumber);
            break;
        default:
            printf("LoadValue() is called with an unsupported valueType\n");
            exit(-1);
    }
        
	return iValue;
};

/* this is the initialization process */
void initializeMotorPositions(void) {

	/* first we need to move the motorX to a known position */
	int i, previouslyActive;
	for (i=0; i<totalStepsX; i++) {
		/* sensor aktiv, wir wissen aber die genaue Position nicht */
		if (getSensor(SensorX) == active) {
			previouslyActive = active;
			moveX(1);
			printf ("SensorX aktiv");
		}

		/* Jetz wissen wir dass wir 1en Schritt neben dem Sensor sind */
		else if (getSensor(SensorX) == passive && previouslyActive == active) {
			moveX(toStartX);
			printf ("SensorX in Ausgangsposition");
			break;
		}

		else {
			printf ("SensorX inaktiv");
			moveX(-1);
		}
	}

	/* first we need to move the motorX to a known position */
	i=0;
	previouslyActive=passive;
	for (i=0; i<totalStepsY; i++) {
		/* sensor aktiv, wir wissen aber die genaue Position nicht */
		if (getSensor(SensorY) == active) {
			previouslyActive = active;
			moveY(1);
			printf ("SensorY aktiv");
		}

		/* Jetz wissen wir dass wir 1en Schritt neben dem Sensor sind */
		else if (getSensor(SensorY) == passive && previouslyActive == active) {
			moveY(toStartY);
			printf ("SensorY in Ausgangsposition");
			break;
		}

		else {
			printf ("SensorY inaktiv");
			moveY(-1);
		}
	}



};
